import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import Dashboard from '@/views/Dashboard.vue'
import { isTokenExpired } from '@/utils/auth'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: Home,
    redirect: '/dashboard',
    meta: { requireAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '首页', requireAuth: true }
      },
      // 请假相关路由（暂时占位，后续实现）
      {
        path: '/leave/apply',
        name: 'LeaveApply',
        component: () => import('@/views/leave/Apply.vue'),
        meta: { title: '请假申请', requireAuth: true }
      },
      {
        path: '/leave/my-list',
        name: 'MyLeaveList',
        component: () => import('@/views/leave/MyList.vue'),
        meta: { title: '我的请假', requireAuth: true }
      },
      {
        path: '/leave/approve',
        name: 'LeaveApprove',
        component: () => import('@/views/leave/Approve.vue'),
        meta: { title: '请假审批', requireAuth: true, roles: [2, 3] }
      },
      {
        path: '/leave/approve-history',
        name: 'ApproveHistory',
        component: () => import('@/views/leave/ApproveHistory.vue'),
        meta: { title: '审批历史', requireAuth: true, roles: [2, 3] }
      },
      {
        path: '/leave/all-list',
        name: 'AllLeaveList',
        component: () => import('@/views/leave/AllList.vue'),
        meta: { title: '全部请假', requireAuth: true, roles: [3] }
      },
      {
        path: '/user/manage',
        name: 'UserManage',
        component: () => import('@/views/user/Manage.vue'),
        meta: { title: '员工管理', requireAuth: true, roles: [3] }
      },
      {
        path: '/dept/manage',
        name: 'DeptManage',
        component: () => import('@/views/dept/Manage.vue'),
        meta: { title: '部门管理', requireAuth: true, roles: [3] }
      },
      {
        path: '/system/setting',
        name: 'SystemSetting',
        component: () => import('@/views/system/Setting.vue'),
        meta: { title: '系统设置', requireAuth: true, roles: [3] }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 员工请假管理系统` : '员工请假管理系统'
  
  // 检查是否需要登录
  if (to.meta.requireAuth) {
    // 检查 token 是否存在和是否过期
    if (isTokenExpired()) {
      ElMessage.warning('登录已过期，请重新登录')
      next('/login')
      return
    }
    
    // 检查角色权限
    if (to.meta.roles) {
      const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
      if (!to.meta.roles.includes(userInfo.roleType)) {
        ElMessage.error('您没有权限访问该页面')
        next('/')
        return
      }
    }
    
    next()
  } else if (to.path === '/login') {
    // 已登录状态访问登录页，跳转到首页
    if (!isTokenExpired()) {
      next('/')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
